package me.jiatao.javestudy.thread;

import java.util.concurrent.Callable;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;


/**
 * Created by JiaTao on 2018/9/11.
 * Email: 535318720@qq.com
 * Version: 1.0
 * Description:
 */


public class ThreadMain {


    int x = 0;


    class MyThread extends Thread {
        @Override
        public void run() {
            int y = 1 / 0;
            for (int i = 0; i < 500; i++) {
                x++;
                System.err.println(Thread.currentThread().getName() + "------>" + x);
            }
        }
    }

    class MyRunnable implements Runnable {
        private String splitString;

        public MyRunnable() {
            splitString = "******";
        }

        public MyRunnable(String splitString) {
            this.splitString = splitString;
        }

        @Override
        public void run() {
            System.err.println(Thread.currentThread().getName() + splitString + "run11111111111111111111");
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
//            System.err.println(Thread.currentThread().getName() + splitString + x);
            System.err.println(Thread.currentThread().getName() + splitString + "run2222222222222");
//            for (int i = 0; i < 500; i++) {
//                x++;
//
//            }
        }
    }

    public void createThread() {
        try {
            MyThread thread = new MyThread();
            thread.start();
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        new Thread(new MyRunnable()).start();
    }


    class SumTask implements Callable<Long> {
        @Override
        public Long call() throws Exception {
            System.err.println(Thread.currentThread().getName() + "------>  call");
            Thread.currentThread().sleep(2000);
            long sum = 0;
            for (int i = 0; i < 300; i++) {
                sum += i;
            }
            return sum;
        }
    }

    public void createCallable() {
        FutureTask<Long> futureTask = new FutureTask<>(new SumTask());
//        futureTask.run();
        new Thread(futureTask).start();
        try {
            long res = futureTask.get(1, TimeUnit.SECONDS);
            System.err.println(res);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void threadPool() {
//        Executor executor;
//        ExecutorService executorService;
//        AbstractExecutorService abstractExecutorService;
//        DelegatedExecutorService delegatedExecutorService;
//        SingleThreadExecutor singleThreadExecutor;
        Executors executors;


//            public ThreadPoolExecutor(int corePoolSize,
//        int maximumPoolSize,
//        long keepAliveTime,
//        TimeUnit unit,
//        BlockingQueue<Runnable> workQueue,
//        ThreadFactory threadFactory,
//        RejectedExecutionHandler handler)
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(3, 5,
                10, TimeUnit.SECONDS, new LinkedBlockingQueue()
                , new RejectedExecutionHandler() {
            @Override
            public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {

            }
        });

//        threadPoolExecutor.execute(new MyRunnable());
//        threadPoolExecutor.execute(new MyRunnable("%%%%%%%%%%%%%%%%%"));

        Future<Long> future = threadPoolExecutor.submit(new SumTask());
        try {
            long res = future.get();
            System.err.println(res);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void scheduledExecutorService() {
        ScheduledExecutorService executorService = Executors.newScheduledThreadPool(2);
        executorService.scheduleWithFixedDelay(new MyRunnable(), 3, 1, TimeUnit.SECONDS);

    }

}